package refactortips.editors;

import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.handlers.IHandlerService;

public class CommandMouseAdapter extends MouseAdapter {
	
	private IWorkbenchPartSite site;
	private String commandId;

	public CommandMouseAdapter(IWorkbenchPartSite site, String commandId){
		this.site = site;
		this.commandId = commandId;
	}
	
	@Override
	public void mouseUp(MouseEvent e) {
		Command command = ((ICommandService) this.site.getService(ICommandService.class)).getCommand(this.commandId);
		final Event trigger = new Event();
		ExecutionEvent executionEvent = ((IHandlerService) this.site.getService(IHandlerService.class)).createExecutionEvent(command, trigger);
		try {
			command.executeWithChecks(executionEvent);
		} catch (Exception exc) {
			exc.printStackTrace();
		}
	}
}
